package Demo04.Demo03.Demo01;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;


public class Demo03_List{
	
	public static void main(String[] args){
		
		LinkedList<Student> lt=new LinkedList<Student>();
		for(int i=20070301,j=10;i<=20070330;i++,j++)
	    {
	        lt.add(new Student(i,(int) (40*Math.random()+60), "同学"+j));
	    }
		
		//对链表排序
		Collections.sort(lt, new Sortbygrade());
		//输出链表
		for(Student sd:lt)
			System.out.println(sd);
		
	}
	
}
 
 
 
//学生类
class Student{
	int num,grade;
	String name;
	
	//构造函数
	public Student(int num,int grade,String name){
		
		this.num=num;
		this.name=name;
		this.grade=grade;
	}
	
	//此处必须覆写
	public String toString(){
//		System.out.println("hi");
		return "学号："+this.num+"\t"+"姓名:"+this.name+"    "+"成绩:"+this.grade;
	}
	
}
 
//创建一个比较器类
class Sortbygrade implements Comparator<Student>{
 
	@Override
	public int compare(Student s1, Student s2) {
		
		if(s1.grade>s2.grade)
			return 1;
		if(s1.grade<s2.grade)
			return -1;
		if(s1.grade==s2.grade)
			return s1.name.compareTo(s2.name);
		return 0;		
		
	}
	
}